package kekeke;

import com.sun.j3d.utils.geometry.Primitive;
import com.sun.j3d.utils.pickfast.PickCanvas;

import javax.media.j3d.Canvas3D;
import javax.media.j3d.PickInfo;
import javax.media.j3d.Shape3D;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.GeneralPath;

public class Kekekeke extends MouseAdapter {

    private JFrame frame;
    private PickCanvas pickCanvas;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Kekekeke window = new Kekekeke();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public Kekekeke() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        final JPanel panel = new JPanel(new BorderLayout());
        panel.setBounds(10, 11, 309, 240);
        GeneralPath star = new GeneralPath();
        int r = 10;
        star.moveTo((20 + r), 0);
        for (int i = 20; i < 360; i += 20) {
            r = -r;
            star.lineTo((float) Math.cos(Math.toRadians(i)) * (20 + r), (float) Math.sin(Math.toRadians(i)) * (20 + r));
        }
        star.closePath();
        final CurvedShape extrude = new CurvedShape(star);
        extrude.getCanvas3D().addMouseListener(this);
        final Canvas3D kk = extrude.getCanvas3D();
        pickCanvas = extrude.pickCanvas;
        panel.add(kk, BorderLayout.CENTER);
        frame.getContentPane().add(panel);
        final JButton btnOops = new JButton("oops");
        btnOops.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                extrude.sceneBranch.detach();
            }
        });
        btnOops.setBounds(329, 11, 95, 23);
        frame.getContentPane().add(btnOops);
    }

    public void mouseClicked(MouseEvent e) {
        pickCanvas.setShapeLocation(e);
        PickInfo result = pickCanvas.pickClosest();
        if (result == null) {
            System.out.println("Nothing picked");
        } else {
            Primitive p = (Primitive) result.getNode();
            Shape3D s = (Shape3D) result.getNode();
            if (p != null) {
                System.out.println(p.getClass().getName());
            } else if (s != null) {
            } else {
                System.out.println("null");
            }
        }
    }
}
